<?php

App::uses('Crud', 'Model');
App::uses('Area', 'Model');
App::uses('Moe', 'Model');
App::uses('User', 'Model');
App::uses('Test', 'Model');
App::uses('Manager', 'Model');
App::uses('UserArea', 'Model');
App::uses('Categorization', 'Model');

class Evaluation extends Crud {

    public $useTable = 'evaluations';

    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const TRAINING_NOK = 0;
    const TRAINING_OK = 1;
    const SUCCESS_OK = 1;
    const SUCCESS_NOK = 0;

    public function getEnabledByUserAndTest($userId, $testId) {
        $filters = array(
            'conditions' => array(
                'user_id' => $userId,
                'test_id' => $testId,
                'status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'creation_date' => 'DESC'
            ),
        );
        return $this->find('first', $filters);
    }

    public function getSuccessByUser($userId) {
        $filters = array(
            'fields' => array(
                'Evaluation.id as value',
                't.name as test',
                'MAX(at.qualification) as calif',
                'MAX(at.application_date) as dateEval'
            ),
            'joins' => array(
                array(
                    'table' => 'attempts',
                    'alias' => 'at',
                    'type' => 'inner',
                    'conditions' => array(
                        'at.evaluation_id = Evaluation.id'
                    ),
                ),
                array(
                    'table' => 'tests',
                    'alias' => 't',
                    'type' => 'inner',
                    'conditions' => array(
                        't.id = Evaluation.test_id'
                    ),
                ),
            ),
            'conditions' => array(
                'Evaluation.user_id' => $userId,
                'Evaluation.status' => self::STATUS_ENABLED,
                'Evaluation.success' => self::SUCCESS_OK,
            ),
            'group' => array(
                'Evaluation.id',
                't.name',
            ),
            'order' => array(
                'Evaluation.creation_date' => 'DESC'
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    public function getInfoDiplomas() {
        $filters = array(
            'fields' => array(
                'Evaluation.id as idEval',
                'u.full_name as nameU',
                'm.name as nameM',
                'a.name as nameA',
                't.name as nameT',
                'Evaluation.creation_date as date',
                'mgrh.signature as rh_sign',
                'mgrh.job as rh_job',
                'urh.full_name as rh_name',
                'mgmoe.signature as moe_sign',
                'mgmoe.job as moe_job',
                'umoe.full_name as moe_name',
                'mgts.signature as ts_sign',
                'mgts.job as ts_job',
                'uts.full_name as ts_name'
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'u.id = Evaluation.user_id',
                        'u.status' => User::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'tests',
                    'alias' => 't',
                    'type' => 'inner',
                    'conditions' => array(
                        't.id = Evaluation.test_id',
                        't.status' => Test::STATUS_ENABLED
                    ),
                ),
                array(
                    'table' => 'areas',
                    'alias' => 'a',
                    'type' => 'inner',
                    'conditions' => array(
                        'a.id = t.area_id',
                        'a.status' => Area::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'moes',
                    'alias' => 'm',
                    'type' => 'inner',
                    'conditions' => array(
                        'm.id = a.moe_id',
                        'm.status' => Moe::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'managers',
                    'alias' => 'mgrh',
                    'type' => 'left',
                    'conditions' => array(
                        'mgrh.type' => Manager::MANAGER_RH,
                        'mgrh.status' => Manager::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'urh',
                    'type' => 'left',
                    'conditions' => array(
                        'urh.id = mgrh.user_id',
                        'urh.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'managers',
                    'alias' => 'mgmoe',
                    'type' => 'left',
                    'conditions' => array(
                        'mgmoe.moe_id = m.id',
                        'mgmoe.type' => Manager::MANAGER_MOE,
                        'mgmoe.status' => Manager::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'umoe',
                    'type' => 'left',
                    'conditions' => array(
                        'umoe.id = mgmoe.user_id',
                        'umoe.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'managers',
                    'alias' => 'mgts',
                    'type' => 'left',
                    'conditions' => array(
                        'mgts.moe_id = m.id',
                        'mgts.type' => Manager::TRAINING_SUPERVISOR,
                        'mgts.status' => Manager::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'uts',
                    'type' => 'left',
                    'conditions' => array(
                        'uts.id = mgts.user_id',
                        'uts.status' => User::STATUS_ENABLED
                    )
                )
            ),
            'conditions' => array(
                'Evaluation.status' => self::STATUS_ENABLED,
                'Evaluation.success' => self::SUCCESS_OK
            )
        );

        return $this->flatArray($this->find('all', $filters));
    }

    public function getEvaluationsRequiresAproval($areas) {
        $filters = array(
            'fields' => array(
                'u.id as user',
                'cat.id as cat',
                'Evaluation.id as eval',
                'u.nomina as nomina',
                'u.full_name as name',
                't.name as test',
                'cat.name as level',
                'l.name as line'
            ),
            'joins' => array(
                array(
                    'table' => 'categorizations',
                    'alias' => 'cat',
                    'type' => 'inner',
                    'conditions' => array(
                        'cat.test_id = Evaluation.test_id',
                        'cat.requires_approval' => Categorization::REQUIERES_APROVAL,
                        'cat.status' => Categorization::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'user_areas',
                    'alias' => 'ua',
                    'type' => 'inner',
                    'conditions' => array(
                        'ua.area_id' => $areas,
                        'ua.user_id = Evaluation.user_id',
                        'ua.status' => UserArea::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'tests',
                    'alias' => 't',
                    'type' => 'inner',
                    'conditions' => array(
                        't.id = cat.test_id',
                        't.status' => Test::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'u',
                    'type' => 'inner',
                    'conditions' => array(
                        'u.id = Evaluation.user_id',
                        'u.status' => User::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'inner',
                    'conditions' => array(
                        'l.id = cat.line_id',
                        'l.status' => Line::STATUS_ENABLED
                    )
                ),
                array(
                    'table' => 'level_approvals',
                    'alias' => 'la',
                    'type' => 'left',
                    'conditions' => array(
                        'la.evaluation_id = Evaluation_id',
                        'la.categorization_id = cat.id',
                        'la.user_id = u.id',
                        'la.status' => 1
                    )
                )
            ),
            'group' => array(
                'Evaluation.id',
                'u.id',
                'cat.id',
                'Evaluation.id',
                'u.nomina',
                'u.full_name',
                't.name',
                'cat.name',
                'l.name'
            ),
            'conditions' => array(
                'la.id IS NULL',
                'Evaluation.status' => self::STATUS_ENABLED,
                'Evaluation.success' => self::SUCCESS_OK
            )
        );

        return $this->flatArray($this->find('all', $filters));
    }

}
